package bman.game.engine;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.util.List;
import java.util.logging.Logger;

import javax.swing.JPanel;

public class GraphicsEngine extends JPanel implements Runnable
{
	Logger log = Logger.getLogger(this.getClass().getName());
	
	public static int WIDTH = 400;   
    public static int HEIGHT = 400;

	boolean run = true;
	Thread thread;
	
	Image bufferImage;
	Graphics bufferGraphics;
	GameMap map;
	TextFeed textFeed1 = new TextFeed();
	TextFeed textFeed2 = new TextFeed();
	
	Timer timer = new Timer();
	
	public GraphicsEngine()
	{
		setBackground(Color.white);
	    setSize(new Dimension(WIDTH, HEIGHT));
	    setTextFeed(new TextFeed());
	}
	
	public void setPreferredSize(Dimension d)
	{
		WIDTH = d.width;
		HEIGHT = d.height;
		bufferImage = createImage(d.width,d.height);
		this.setSize(d);
		super.setPreferredSize(d);
	}
	
	public static Timer getTimer()
	{
		return new Timer();
	}
	
	public void addNotify()
	{
		super.addNotify();
		thread = new Thread(this);
		thread.start();
	}
	
	public GameMap loadMap(String file)
	{
		map = new GameMap();
		map.load(file);
		return map;
	}
	
	public void setMap(GameMap map)
	{
		this.map = map;
	}
	
	public void setTextFeed(TextFeed textFeed)
	{
		this.textFeed2 = textFeed;
	}

	public void paintComponent(Graphics g)
	{
		g.drawImage(bufferImage,0,0, null);
	}
	
	public void drawMap(GameMap map)
	{
		if(map == null)
			return;
		
		List l = map.getEntities();
		for(int i=0;i<l.size();i++)
		{
			Entity e = (Entity)l.get(i);
			log.finer("Drawing...:"+e.getType());
			Image img = ImageLibrary.getImage(e.getType(),e.getBearing());
			bufferGraphics.drawImage(img,e.x() ,e.y(), null);
		}
	}
	
	public void drawTexts()
	{
		if(textFeed2 == null)
		{
			log.info("No text feed set.");
			return;
		}
		
		List<String> s = textFeed1.getTexts();
		FontMetrics fm = bufferGraphics.getFontMetrics();
		int padding = 30; 
		int height = 15;
		for (String text : s)
		{
			int width = fm.stringWidth(text);			
			
			bufferGraphics.setColor(Color.black);
			bufferGraphics.drawString(text, this.WIDTH-width-padding,height);
			height += fm.getHeight();;
		}
		
		s = textFeed2.getTexts();
		for (String text : s)
		{
			int width = fm.stringWidth(text);			
			
			bufferGraphics.setColor(Color.black);
			bufferGraphics.drawString(text, this.WIDTH-width-padding,height);
			height += fm.getHeight();;
		}
	}
	
	int fpsIndex = 0;
	
	public void run()
	{
		log.info("GraphicsEngine starting...");
		
		while(run)
		{			
			try
			{
				timer.update();
				render();
				repaint();
				
				thread.sleep(timer.getSleep());
				fpsIndex = textFeed1.setText(fpsIndex,"FPS: "+timer.getFPS());
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
    private void render()
    {
		if(bufferImage == null)
		{
			bufferImage = createImage(WIDTH,HEIGHT);
			
			if(bufferImage == null)
			{
				log.info("bufferImage is null exiting method.");
				return;
			}
		}
		
		bufferGraphics = bufferImage.getGraphics();
		
		bufferGraphics.setColor(Color.white);
		bufferGraphics.fillRect (0, 0, WIDTH,HEIGHT);
		
		if(bufferGraphics == null)
		{
			log.info("bufferGraphics is null exiting method.");
		}
		
		drawMap(map);
		drawTexts();
    }
}


